package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.jopo.PageBean;
import com.itheima.jopo.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * StudentServiceImpl
 *
 * @author liliudong
 * @version 1.0
 * @description StudentService
 * @date 2023/5/1 11:04
 */
@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {

    private final StudentMapper studentMapper;

    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setDisciplineTimes(0);
        student.setDisciplineScore(0);
        studentMapper.insert(student);
    }

    @Override
    public void removeById(Integer id) {
        studentMapper.deleteById(id);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void updateById(Student student) {
        if (Objects.isNull(student.getId())) {
            return;
        }
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }

    @Override
    public void deductions(Student student) {
        Student studentExist = this.getById(student.getId());
        if (Objects.isNull(studentExist)) {
            return;
        }
        studentExist.setDisciplineTimes(studentExist.getDisciplineTimes() + 1);
        studentExist.setDisciplineScore(studentExist.getDisciplineScore() + student.getDisciplineScore());
        updateById(studentExist);
    }

    @Override
    public Boolean existByPhone(String phone) {
        Student student = new Student();
        student.setPhone(phone);
        return studentMapper.selectCount(student) > 0;

    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Integer highestDegree, Integer classesId, String studentNumber) {
        PageHelper.startPage(page, pageSize);
        Student student = new Student();
        student.setName(name);
        student.setHighestDegree(highestDegree);
        student.setClassesId(classesId);
        student.setStudentNumber(studentNumber);
        List<Student> studentList = studentMapper.selectList(student);
        PageInfo<Student> studentPageInfo = new PageInfo<>(studentList);
        return new PageBean(studentPageInfo.getTotal(), studentPageInfo.getList());
    }

    @Override
    public Boolean existByStudentNumber(String studentNumber) {
        Student student = new Student();
        student.setStudentNumber(studentNumber);
        return studentMapper.selectCount(student) > 0;
    }

    @Override
    public Boolean checkPhoneRepeat(Integer id, String phone) {
        Student student = new Student();
        student.setPhone(phone);
        return studentMapper.selectCount(student) > 0;
    }

    @Override
    public Boolean checkStudentNumberRepeat(String studentNumber) {
        Student student = new Student();
        student.setStudentNumber(studentNumber);
        return studentMapper.selectCount(student) > 0;
    }

}
